home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************/
- /* */
- /* putenv(3) */
- /* */
- /* Change or add an environment entry */
- /* */
- /****************************************************************/
- /* origination 1987-Oct-7 T. Holm */
- /****************************************************************/
-
- #include "lib.h"
- #include <stdio.h>
-
-
- #define PSIZE sizeof(char *)
-
-
- extern char **environ;
-
- #ifndef __STDC__
- char *index();
- char *malloc();
- #endif
-
- /****************************************************************/
- /* */
- /* putenv( entry ) */
- /* */
- /* The "entry" should follow the form */
- /* "NAME=VALUE". This routine will search the */
- /* user environment for "NAME" and replace its */
- /* value with "VALUE". */
- /* */
- /* Note that "entry" is not copied, it is used */
- /* as the environment entry. This means that it */
- /* must not be unallocated or otherwise modifed */
- /* by the caller, unless it is replaced by a */
- /* subsequent putenv(). */
- /* */
- /* If the name is not found in the environment, */
- /* then a new vector of pointers is allocated, */
- /* "entry" is put at the end and the global */
- /* variable "environ" is updated. */
- /* */
- /* This function normally returns 0, but -1 */
- /* is returned if it can not allocate enough */
- /* space using malloc(3), or "entry" does not */
- /* contain a '='. */
- /* */
- /****************************************************************/
-
-
- int putenv( entry )
- char *entry;
-
- {
- unsigned long length;
- unsigned long size;
- char **p;
- char **new_environ;
-
- /* Find the length of the "NAME=" */
-
- if ( (length=(unsigned long) index(entry,'=')) == (unsigned long)NULL )
- return( -1 );
-
- length = length - (unsigned long) entry + 1;
-
-
- /* Scan through the environment looking for "NAME=" */
-
- for ( p=environ; *p != 0 ; p++ )
- if ( strncmp( entry, *p, (int)length ) == 0 )
- {
- *p = entry;
- return( 0 );
- }
-
-
- /* The name was not found, build a bigger environment */
-
- size = (unsigned long)(p - environ);
-
- new_environ = (char **) malloc( (unsigned)((size+2)*PSIZE) );
-
- if ( new_environ == NULL )
- return( -1 );
-
- bcopy( (char *) environ, (char *) new_environ, (long)(size*PSIZE) );
-
- new_environ[size] = entry;
- new_environ[size+1] = NULL;
-
- environ = new_environ;
-
- return(0);
- }
-